<?php

/***
 * Class AdminRolesModel
 * 用户权限关联表
 */
class AdminRolesModel extends PdoModel {
    protected $_name = 'admin_roles';

    function __construct(){
        parent::__construct();
    }

    #插入
    function addAllData($uid, $data){
        $curtime = time();
        if(empty($data)){
            return false;
        }

        foreach ($data as $key => $val) {
            $data = array('uid' => $uid, 'access_id' => $val, 'created_at' => $curtime);
            $this->insert($data);
        }
        return $key;
    }

    #根据UID删除数据
    function deleteByUid($uid){
        if(empty($uid)){
            return false;
        }
        return $this->delete("uid = {$uid}");
    }

    #获取用户的全部的权限
    function getAllAccessList(){
        $sql = " select * from admin_access limit 1000";

        try{
            $data = $this->fetchAll($sql);
            return $data;

        }catch(Exception $ex){
            return array();
        }
    }


    #根据UID获取数据
    function getListByUid($uid){
        if(empty($uid)){
            return false;
        }

        $sql = " select * from  {$this->_name} where uid = {$uid} ";
        try{
            $data = $this->fetchAll($sql);
            return $data;
        }catch(Exception $ex){
            return array();
        }
    }

    #是否拥有权限
    function checkPermiss($uid, $m, $c, $a){
        if(empty($uid) || empty($m) || empty($c) || empty($a)){
            return false;
        }
        $sql = " select u.id, u.uid from admin_roles u, admin_access c where u. access_id =c.id and u.uid={$uid} and c.m='{$m}' and c.c='{$c}' and c.a='{$a}'";
        $result = $this->fetchRow($sql);
        if(!empty($result['id'])) {
            return $result;
        } else {
            return array();
        }
    }
}
